package me.wz.util.sdbc.dialect;

import me.wz.util.sdbc.IDialect;
import me.wz.util.sdbc.Page;
import me.wz.util.sdbc.Pagination;

public class Mysql implements IDialect {
    @Override
    public String count(String select, Pagination pagination) {
        if( !pagination.getHasMoreMode() ) {
            return "select count(1) from (" + select + ")a";
        }
        return null;
    }

    @Override
    public String query(String select, Pagination pagination) {
        int limit = Math.abs(pagination.getPageSize()) + (pagination.getHasMoreMode() ? 1 : 0);
        int offset = Math.max(0, pagination.getPageNum() - 1) * Math.abs(pagination.getPageSize());
        return "select * from (" + select + ")a " + (isBlank(pagination.getOrderBy()) ? "" : "order by " + pagination.getOrderBy())  + " limit " + offset + "," + limit;
    }

    @Override
    public <T>Page<T> revert(Page<T> page) { return page; }

    protected boolean isBlank(String str){ return str == null || str.trim().equals(""); }
}
